package com.cloudcc.mobile.weight.ui;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

/**
 * @Description: 用户头像图片
 * @author wangzhiqiang
 * @date 2015-12-9 下午1:10:27
 * 
 */
public class UserLogoDrawable extends Drawable {
	protected Paint mPaint;
	protected Paint mTextPaint;
	protected int backgroundColor;
	protected int foregroundColor;
	protected String name;
	protected int mRadius = 0;

	public UserLogoDrawable(int backgroundColor, int textColor, String name) {
		super();
		this.foregroundColor = textColor;
		this.backgroundColor = backgroundColor;
		this.name = name;
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setColor(backgroundColor);
		mPaint.setStyle(Style.FILL);

		mTextPaint = new Paint();
		mTextPaint.setAntiAlias(true);
		mTextPaint.setColor(foregroundColor);
		mTextPaint.setStyle(Style.FILL);
		mTextPaint.setTextAlign(Align.CENTER);
		mTextPaint.setStrokeWidth(2);

	}

	@Override
	public void draw(Canvas canvas) {
		Rect bounds = getBounds();
		canvas.drawCircle(bounds.centerX(), bounds.centerY(), mRadius, mPaint);
		mTextPaint.setTextSize(mRadius * 2 / 3);
		canvas.drawText(name, bounds.centerX(),
				bounds.centerY() + mTextPaint.getTextSize() / 2 - 2, mTextPaint);
	}

	@Override
	protected void onBoundsChange(final Rect bounds) {
		super.onBoundsChange(bounds);
		mRadius = Math.min(bounds.width(), bounds.height()) / 2;
	}

	@Override
	public void setAlpha(int alpha) {
		mPaint.setAlpha(alpha);
	}

	@Override
	public void setColorFilter(ColorFilter cf) {
		mPaint.setColorFilter(cf);
	}

	@Override
	public int getOpacity() {
		return PixelFormat.TRANSLUCENT;
	}

}
